﻿using System.Web;

namespace App.Utility.StateContainers
{
    /// <summary>
    /// Simple HttpApplicationState Container written by Charles Cook
    /// 
    /// Tuned on 5/12/08 by Charles
    /// </summary>
    public class AppStateContainer<T>
    {
        HttpApplicationState AppState;
        string m_key;
        public AppStateContainer(string Key, HttpApplicationState bag)
        {
            m_key = Key;
            AppState = bag;
            //cost:0.0397s
            //HttpContext.Current.Trace.Warn("Key '" + Key + "' is " + ((bag[Key] == null) ? "NOT FOUND" : "found"));
        }

        public AppStateContainer(string Key, HttpApplicationState bag, T defaultValue)
        {
            m_key = Key;
            AppState = bag;
            if (AppState[m_key] == null)
                Value = defaultValue;
        }

        public T Value
        {
            get
            {
                //cost:0.0765s
                //HttpContext.Current.Trace.Warn("Key '" + m_key + "' is " + ((AppState[m_key] == null) ? "NOT FOUND" : "found"));
                if (AppState[m_key] == null)
                    return default(T);
                return (T)AppState[m_key];
            }
            set
            {
                //cost:0.0001s
                //HttpContext.Current.Trace.Warn("Setting Key '" + m_key + "'");
                AppState[m_key] = value;
            }
        }
    }
}